package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.permission.util;

import com.kingyun.gpsinspection.purificationservice.common.bean.ApplicationContextHelper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Menu;
import com.kingyun.gpsinspection.purificationservice.facade.model.webservicesmodel.UasMenu;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmMenuService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class MenuPool implements Runnable {
    private List<UasMenu> list;
    private String controlType;//1.添加，2.修改，3.删除

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    public void setList(List<UasMenu> list) {
        this.list = list;
    }

    @Override
    public void run() {
        JsmMenuService menuService = (JsmMenuService) ApplicationContextHelper.getBean("menuService");
        if (menuService != null && StringUtils.isNotEmpty(controlType)) {
            List<Menu> paramList = new ArrayList<Menu>();
            try {
                menu(list, paramList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {//处理菜单信息
                if ("1".equals(controlType)) {
                    //添加
                    menuService.insertList(paramList);
                } else if ("2".equals(controlType)) {
                    //修改
                    menuService.updateList(paramList);
                } else if ("3".equals(controlType)) {
                    //删除
                    menuService.deleteList(paramList);
                }
            }
        }
    }

    /**
     * 处理菜单信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void menu(List<UasMenu> list, List<Menu> paramList) throws Exception {
        if (list != null && list.size() > 0) {
            ConvertUtils.register(new DateConverter(null), Date.class);
            Date date = new Date();
            for (UasMenu um : list) {
                Menu param = new Menu();
                BeanUtils.copyProperties(param, um);
                param.setUrl(um.getMenuUrl());
                param.setParentMenuId(um.getParentId());
                param.setMenuStyle("");
                param.setSystemPlatformId("");
                param.setModulesPermission("");
                param.setMenuOrder(1);
                param.setImplClass("");
                param.setPrieCalss("");
                param.setRemark1("");
                param.setRemark2("");
                param.setStatus(0);
                param.setModifyDate(date);
                param.setCreateDate(date);
                param.setRemark(um.getMemo());
                param.setImgPath(um.getIconPath());
                paramList.add(param);
            }
        }
    }

}
